package abq2.jic.jic.plugin.operate.course;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.LocaleString;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.servicehelper.workflow.MessageCenterServiceHelper;
import kd.bos.workflow.engine.msg.info.MessageInfo;
import kd.sdk.plugin.Plugin;

import java.util.*;

/**
 * 后台任务插件
 */
public class CourseDailyTask extends AbstractTask implements Plugin {
    /**
     * 获取当前日期是星期几
     * @param date
     * @return 当前日期是星期几
     */
    public String getWeekOfDate(Date date) {
        String[] weekDays = { "7", "1", "2", "3", "4", "5", "6" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];

    }

    @Override
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        HashMap<String, String> hashMap = new HashMap<>();

        Date date = new Date();
        String weekOfDate = getWeekOfDate(date);

        QFilter idFilter = new QFilter("abq2_textfield3", QCP.equals,weekOfDate);


        DynamicObject[] optionCourses = BusinessDataServiceHelper.load("abq2_my_course",
                "abq2_my_course_stu," + //课程内码
                        "name," + //课程名称
                        "abq2_textfield3," + //星期
                        "abq2_textfield1," + //教室
                        "abq2_textfield," + //教师
                        "abq2_textfield4," + //年级
                        "creator," + //学生
                        "abq2_textfield2",  //课节
                new QFilter[]{new QFilter("creator",QCP.equals, RequestContext.get().getCurrUserId()),idFilter});

        JSONArray jsonArray = new JSONArray();
        for (DynamicObject optionCourse : optionCourses) {
            JSONObject jsonObject = new JSONObject();
            String name = optionCourse.getString("name");
            String classTimes = optionCourse.getString("abq2_textfield2").replaceAll(",","  ");
            String classroom = optionCourse.getString("abq2_textfield1");
            jsonObject.put("courseName",name);
            jsonObject.put("classTimes",classTimes);
            jsonObject.put("classroom",classroom);
            jsonArray.add(jsonObject);
        }
        hashMap.put("courseToday",jsonArray.toJSONString());

        //调用GPT开发平台微服务
        Object[] params = new Object[] {
                //GPT提示编码
                getPromptFid("prompt-2407121131129B"),
                "",
                hashMap
        };
        Map<String, Object> result = DispatchServiceHelper.invokeBizService("ai", "gai", "GaiPromptService", "syncCall", params);
        JSONObject jsonObjectResult = new JSONObject(result);
        JSONObject jsonObjectData = jsonObjectResult.getJSONObject("data");
        String llmValue = jsonObjectData.getString("llmValue");

        sendMessage(requestContext,llmValue);
    }

    public long getPromptFid(String billNo) {
        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("gai_prompt",
                "number," +
                        "id",
                (new QFilter("number", QCP.equals, billNo)).toArray());
        return dynamicObject.getLong("id");
    }

    private void sendMessage(RequestContext requestContext,String message){
        MessageInfo messageInfo = new MessageInfo();
        LocaleString title = new LocaleString();
        title.setLocaleValue_zh_CN("今日课程信息");
        messageInfo.setMessageTitle(title);
        LocaleString context = new LocaleString();
        context.setLocaleValue_zh_CN(message);
        messageInfo.setMessageContent(context);

        ArrayList<Long> ids = new ArrayList<Long>();

        ids.add(requestContext.getCurrUserId());

        messageInfo.setUserIds(ids);
        messageInfo.setType(MessageInfo.TYPE_NOTICE);
        messageInfo.setTag("课程服务");

        MessageCenterServiceHelper.sendMessage(messageInfo);
    }
}